package com.example.mail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.R;
import core.model.Game;

public class MailBodyActivity extends Activity {

    private LinearLayout mailBobyLayout;
    private String selectedFrom;
    private String selectedMailType;
    private Game game;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.maibodymenu, menu);
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mailbodylayout);

        mailBobyLayout = (LinearLayout) findViewById(R.id.mailBody);

        Bundle bundle = getIntent().getExtras();
        game = (Game) bundle.getSerializable("game");
        selectedMailType = bundle.getString("selectedMailType");

        String selectedTurn = bundle.getString("turn");

        selectedFrom = bundle.getString("from");
        String selectedBody = bundle.getString("body");

        LinearLayout mailBodyLayout = (LinearLayout) findViewById(R.id.mailBody);
        LayoutInflater linflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        View bodyView = linflater.inflate(R.layout.mailbody, null);
        TextView turn = (TextView) bodyView.findViewById(R.id.mailBodyHeaderTurn);
        turn.setText("Turn: " + selectedTurn);

        TextView from = (TextView) bodyView.findViewById(R.id.mailBodyHeaderFrom);
        from.setText("From: " + selectedFrom);

        TextView body = (TextView) bodyView.findViewById(R.id.mailBodyContent);
        body.setText(selectedBody);

        mailBodyLayout.addView(bodyView);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mailBodyMenuReplay:
                showMailReplay();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void showMailReplay() {
        Context context = mailBobyLayout.getContext();
        Intent i = new Intent(context, MailReplayActivity.class);
        i.putExtra("from", selectedFrom);
        i.putExtra("game", game);
        i.putExtra("selectedMailType", selectedMailType);
        context.startActivity(i);
    }
}
